import { Event, EventEmitter, Uri } from "vscode";
import { FileChangeType } from "../../common/platform/fileSystemWatcher";
import { EQEnvInfo, EQEnvKind } from "./info";

export type BasicEQEnvsChangedEvent = {
    kind?: EQEnvKind;
    type?: FileChangeType;
};

export type EQEnvsChangedEvent = BasicEQEnvsChangedEvent & {
    searchLocation?: Uri;
};

export interface IEQEnvsWatcher<E extends BasicEQEnvsChangedEvent = EQEnvsChangedEvent> {
    /**
     * The hook for registering event listeners (callbacks).
     */
    readonly onChanged: Event<E>;
}

export class EQEnvsWatcher<T = EQEnvsChangedEvent> implements IEQEnvsWatcher<T> {
    /**
     * The hook for registering event listeners (callbacks).
     */
    public readonly onChanged: Event<T>;

    private readonly didChange = new EventEmitter<T>();

    constructor() {
        this.onChanged = this.didChange.event;
    }

    /**
     * Send the event to all registered listeners.
     */
    public fire(event: T): void {
        this.didChange.fire(event);
    }
}

export type BasicEQEnvCollectionChangedEvent = {
    old?: EQEnvInfo;
    new?: EQEnvInfo | undefined;
};

export type EQEnvCollectionChangedEvent = BasicEQEnvCollectionChangedEvent & {
    type?: FileChangeType;
    searchLocation?: Uri;
};